<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/

/* Load Table Maintainance API */
xarDBLoadTableMaintenanceAPI();

/**
 * initialise the conversations module
 */
function conversations_init()
{
    /* Set up database tables */
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $ctags = $xartable['ctags'];

    /* Remember the thought process here
     * id => tag id
     * tag => tag name
     * timestamp => build the cloud based on recent data
     * status => moderated discussion (true would be non-shown)
     * uid => who created the tag
     * count => how often viewed
     * options => catch all for serialized data
     */

    $fields = array(
    'xar_id'          => array('type'=>'integer', 
                                'null'=>false,
                                'default'=>'0',
                                'increment'=>true,
                                'primary_key'=>true),
    'xar_tag'         => array('type'=>'varchar', 
                               'null'=>false,
                               'default'=>'',
                               'size'=>255),
    'xar_timestamp'   => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_lastvisit'   => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_status'      => array('type'=>'integer', 
                               'null'=>false, 
                               'default'=>'1', 
                               'size'=>'tiny'),
    'xar_uid'         => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_count'       => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_options'     => array('type'=>'text')
    );

    $query = xarDBCreateTable($ctags,$fields);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $cusers = $xartable['cusers'];

    /* Remember the thought process here
     * uid => user info from roles left join
     * tagcount => number of tags created
     * postcount => number of posts
     * timestamp => last post or tag
     * options => catch all for serialized data
     */

    $fields = array(
    'xar_uid'         => array('type'=>'integer', 
                                'null'=>false,
                                'default'=>'0',
                                'increment'=>true,
                                'primary_key'=>true),
    'xar_tagcount'    => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_postcount'   => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_timestamp'   => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_options'     => array('type'=>'text')
    );

    $query = xarDBCreateTable($cusers,$fields);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $csubs = $xartable['csubs'];

    /* Remember the thought process here
     * uid => user info from roles left join
     * tagcount => number of tags created
     * postcount => number of posts
     * timestamp => last post or tag
     * options => catch all for serialized data
     */

    $fields = array(
    'xar_tagid'       => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_uid'         => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0')
    );

    $query = xarDBCreateTable($csubs,$fields);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $cuvisit = $xartable['cuvisit'];

    /* Remember the thought process here
     * uid => user info from roles left join
     * tagcount => number of tags created
     * timestamp => last visit
     */

    $fields = array(
    'xar_tagid'       => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_uid'         => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_timestamp'   => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0')
    );

    $query = xarDBCreateTable($cuvisit,$fields);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $clook = $xartable['clook'];

    /* Remember the thought process here
     * tid => tagid
     * pid => postid
     */

    $fields = array(
    'xar_tagid'       => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0'),
    'xar_postid'      => array('type'=>'integer',
                               'unsigned'=>TRUE, 
                               'null'=>FALSE, 
                               'default'=>'0')
    );

    $query = xarDBCreateTable($clook,$fields);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $cposts = $xartable['cposts'];

    /* Remember the thought process here
     * postid => id of the post
     * timestamp => when the post was submitted
     * uid => who made the post
     * tagid => what tag this belongs
     * status => has this been moderated
     * options => catch all for serialized data
     */
    $fields = array(
        'xar_postid'    => array('type'=>'integer',  
                                 'null'=>FALSE,  
                                 'increment'=>TRUE,
                                 'primary_key'=>TRUE),
        'xar_timestamp' => array('type'=>'integer',  
                                 'null'=>FALSE),
        'xar_uid'       => array('type'=>'integer',  
                                 'null'=>FALSE,  
                                 'size'=>'medium',
                                 'default'=>1),
        'xar_text'      => array('type'=>'text'),
        'xar_status'    => array('type'=>'integer',
                                 'default'=>1,
                                 'null'=>FALSE,  
                                 'size'=>'tiny'),
        'xar_count'     => array('type'=>'integer',
                                 'unsigned'=>TRUE, 
                                 'null'=>FALSE, 
                                 'default'=>'0'),
        'xar_options'   => array('type'=>'text')
    );

    $query = xarDBCreateTable($cposts,$fields);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    // Index the fields
    // Tags
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_ctag_tags',
        'fields'    => array('xar_tag'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($ctags, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_ctag_timestamp',
        'fields'    => array('xar_timestamp'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($ctags, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_ctag_lastvisit',
        'fields'    => array('xar_lastvisit'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($ctags, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_ctag_status',
        'fields'    => array('xar_status'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($ctags, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_ctag_uid',
        'fields'    => array('xar_uid'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($ctags, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_ctag_count',
        'fields'    => array('xar_count'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($ctags, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    // Users
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_cusers_tagcount',
        'fields'    => array('xar_tagcount'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($cusers, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
        $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_cusers_postcount',
        'fields'    => array('xar_postcount'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($cusers, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
        $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_cusers_timestamp',
        'fields'    => array('xar_timestamp'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($cusers, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    //Subs
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_csubs_tagid',
        'fields'    => array('xar_tagid'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($csubs, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_csubs_uid',
        'fields'    => array('xar_uid'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($csubs, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    //Visits
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_cuvisit_tagid',
        'fields'    => array('xar_tagid'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($cuvisit, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_cuvisit_uid',
        'fields'    => array('xar_uid'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($cuvisit, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    //Look Up
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_clook_tagid',
        'fields'    => array('xar_tagid'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($clook, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_clook_postid',
        'fields'    => array('xar_postid'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($clook, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    // Posts
    $index = array('name'      => 'i_' . xarDBGetSiteTablePrefix() . '_cposts_pid',
                   'fields'    => array('xar_postid'),
                   'unique'    => FALSE);
    $query = xarDBCreateIndex($cposts, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array('name'      => 'i_' . xarDBGetSiteTablePrefix() . '_cposts_status',
                   'fields'    => array('xar_status'),
                   'unique'    => FALSE);
    $query = xarDBCreateIndex($cposts, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array('name'      => 'i_' . xarDBGetSiteTablePrefix() . '_cposts_uid',
                   'fields'    => array('xar_uid'),
                   'unique'    => FALSE);
    $query = xarDBCreateIndex($cposts, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_cposts_count',
        'fields'    => array('xar_count'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($cposts, $index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    // Register Masks
    // Mask = Categorie : Id : Name
    xarRegisterMask('Readcon','All','conversations','Forum','All:All','ACCESS_READ');     // Allows reading Topics
    xarRegisterMask('Viewcon','All','conversations','Read','All:All','ACCESS_OVERVIEW');    // Allows seeing Forum
    xarRegisterMask('Editcon','All','conversations','Forum','All:All','ACCESS_EDIT');
    xarRegisterMask('Addcon','All','conversations','Forum','All:All','ACCESS_ADD');
    xarRegisterMask('Deletecon','All','conversations','Forum','All:All','ACCESS_DELETE');
    xarRegisterMask('Admincon','All','conversations','Forum','All:All','ACCESS_ADMIN');    // Allows all ;D
    xarRegisterMask('Modcon','All','conversations','Forum','All:All','ACCESS_MODERATE');    // Allows Editing Topics
    xarRegisterMask('Postcon','All','conversations','Forum','All:All','ACCESS_COMMENT');  // Allows Posting Replys and Topics

    // modvars
    xarModSetVar('conversations', 'SupportShortURLs', true);
    xarModSetVar('conversations', 'useModuleAlias',false);
    xarModSetVar('conversations', 'aliasname','');

    // hooks
    if (!xarModRegisterHook('item', 'search', 'GUI', 'conversations', 'user', 'search')) {
        return false;
    }
    if (!xarModRegisterHook('item', 'create', 'API', 'conversations', 'user', 'createusers')) {
        return false;
    }
    // No workie...
    if (xarModIsAvailable('registration')) {
        xarModAPIFunc('modules', 'admin', 'enablehooks',
                       array('callerModName' => 'registration','hookModName' => 'conversations'));
    }
    if (xarModIsAvailable('bbcode')) {
        xarModAPIFunc('modules', 'admin', 'enablehooks',
                       array('callerModName' => 'conversations','hookModName' => 'bbcode'));
    }
    if (xarModIsAvailable('search')) {
        xarModAPIFunc('modules', 'admin', 'enablehooks',
                       array('callerModName' => 'search','hookModName' => 'conversations'));
    }
    
    // Grab all of the existing users and put them into the users table.
    $users = xarModAPIFunc('roles', 'user', 'getall', array(
                    'include_anonymous' => false,
                    'include_myself' => false
                ));
    foreach ($users as $user){
        $tagcount = 0;
        $postcount = 0;
        $options = serialize(null);
        $timestamp = time();
        $table = $xartable['cusers'];
        $query = "INSERT INTO $table ("
            . " xar_uid, xar_tagcount, xar_postcount, "
            . " xar_timestamp, xar_options)"
            . " VALUES (?, ?, ?, ?, ?)";
        $result =& $dbconn->Execute($query, array($user['uid'], $tagcount, $postcount, $timestamp, $options));
        if (!$result) return;
    }
    // Need to set this for the cookie bit.
    $settings = array();
    $settings['fresh']     = 60*60*2; // Two hours, right?
    $settings['initial']   = 60*60*24*14; // Two weeks, right?
    xarModSetVar('conversations', 'settings', serialize($settings));
    
    // Ahh, fini.    
    return true;
}

function conversations_delete()
{
    /* Drop the table */
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();

    $cposts = $xartable['cposts'];
    $query = xarDBDropTable($cposts);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $cusers = $xartable['cusers'];
    $query = xarDBDropTable($cusers);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $ctags = $xartable['ctags'];
    $query = xarDBDropTable($ctags);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $csubs = $xartable['csubs'];
    $query = xarDBDropTable($csubs);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $clook = $xartable['clook'];
    $query = xarDBDropTable($clook);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $cuvisit = $xartable['cuvisit'];
    $query = xarDBDropTable($cuvisit);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

   /* Remove any module aliases before deleting module vars */
    $aliasname =xarModGetVar('conversations','aliasname');
    $isalias = xarModGetAlias($aliasname);
    if (isset($isalias) && ($isalias =='conversations')){
        xarModDelAlias($aliasname,'conversations');
    }
    
    // Delete any module variables
    xarModDelAllVars('conversations');
    
    // Remove Masks and Instances
    xarRemoveMasks('conversations');
    xarRemoveInstances('conversations');

    if (!xarModUnregisterHook('item', 'search', 'GUI', 'conversations', 'user', 'search')) {
        return false;
    }
    return true;
}
?>
